package Tabs;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.interactions.Actions;

import MainMethods.FileWr;
import MainMethods.HoverImage;
import MainMethods.Screenshot;

public class ReadBook {
	HoverImage hover = new HoverImage();
	String url;
	Screenshot scsh = new Screenshot();

	public void readTest(WebDriver driver, FileWr file, String screenDir) {
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		// open books tab
		driver.findElement(By.id("books_tab")).click();
		file.writeFile("Start BOOKS test");
		file.writeFile("loading tab 'BOOKS' - " + driver.getCurrentUrl());
		// Click on "add to collection" in any book (plus button)
		try {
			hover.addToCollection(driver);
			try {
				Thread.sleep(4000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if (hover.doesElementExist(driver, By.className("message")) == true) {
				System.out.println("Add Book to Collection - PASSED");
				file.writeFile("Add Book to Collection - PASSED");
				driver.findElement(By.xpath("//*[@id=\"feedback\"]/div[3]")).click();
			} else {
				System.out.println("Add Book to Collection - FAILED");
				file.writeFile("Add Book to Collection - FAILED");
				scsh.makeScreen("Add Book to Collection - FAILED", screenDir);
			}
		} catch (NoSuchElementException e) {
			System.out.println("Add Book to Collection - FAILED");
			file.writeFile("Add Book to Collection NoSuchElementException e - FAILED");
			scsh.makeScreen(
					"Add Book to Collection NoSuchElementException e - FAILED",
					screenDir);
		}
		// Click on the "read" button of any book
		try {
			hover.playHover(driver);
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if (hover.doesElementExist(driver, By.xpath("//*[@id=\"reader-wrap\"]")) == true) {
				System.out
						.println("Read dy click on 'Read Now' button - PASSED");
				file.writeFile("Read dy click on 'Read Now' button - PASSED");
				//scsh.makeScreen("Read dy click on 'Read Now' button PASSED",screenDir);
			} else {
				System.out
						.println("Read dy click on 'Read Now' button - FAILED");
				file.writeFile("Read dy click on 'Read Now' button - FAILED");
				scsh.makeScreen("Read dy click on 'Read Now' button FAILED",
						screenDir);
			}
		} catch (NoSuchElementException e) {
			System.out.println("'Read Now' button Exception e - FAILED");
			file.writeFile("'Read Now' button Exception e - FAILED");
			scsh.makeScreen("'Read Now' button Exception e - FAILED", screenDir);
		}
		// Click on the "Back arrow" in the browser
		driver.navigate().back();
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		// Click on "Read book" on "More info" page
		try {
			if (hover.doesElementExist(driver, By.xpath("//*[@id=\"related_container\"]")) == true) {
				file.writeFile("'More info' page opened");
				System.out.println("'More info' page opened");
				driver.findElement(By.xpath("//*[@id=\"title_download_container\"]/a"))
						.click();
				try {
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				if (hover.doesElementExist(driver, By.xpath("//*[@id=\"reader-wrap\"]")) == true) {
					System.out
							.println("Book read by click on 'Read Book' button on 'More info' page - PASSED");
					file.writeFile("Book read by click on 'Read Book' button on 'More info' page - PASSED");
					//scsh.makeScreen("Book read by click on Read Book button on More info page PASSED",screenDir);
				} else {
					System.out
							.println("Book read by click on 'Read Book' button on 'More info' page - FAILED");
					file.writeFile("Book read by click on 'Read Book' button on 'More info' page - FAILED");
					scsh.makeScreen(
							"Book read by click on Read Book button on More info page FAILED",
							screenDir);
				}
			}
		} catch (NoSuchElementException e) {
			System.out
					.println("'Read Book' button on 'More info' page Exception e - FAILED");
			file.writeFile("'Read Book' button on 'More info' page Exception e - FAILED");
			scsh.makeScreen(
					"'Read Book' button on 'More info' page Exception e - FAILED",
					screenDir);
		}
		driver.navigate().back();
		// Click on the arrow (bottom-left) to go back
//		try {
//			JavascriptExecutor jse = (JavascriptExecutor) driver;
//			jse.executeScript("scroll(250, 0)");
//			Actions hove = new Actions(driver);
//			Actions hoverBtn = hove.moveToElement(driver.findElement(By
//					.xpath("//*[@id=\"reader-wrap\"]/div[3]")));
//			hoverBtn.perform();
//			driver.findElement(By.xpath("//*[@id=\"backToBook\"]")).click();
//			try {
//				Thread.sleep(3000);
//			} catch (InterruptedException e) {
//				e.printStackTrace();
//			}
//			if (hover.doesElementExist(driver, By.xpath("//*[@id=\"related_container\"]")) == true) {
//				System.out
//						.println("Click on the arrow (bottom-left) to go back - PASSED");
//				file.writeFile("Click on the arrow (bottom-left) to go back - PASSED");
//			} else {
//				System.out
//						.println("Click on the arrow (bottom-left) to go back - FAILED");
//				file.writeFile("Click on the arrow (bottom-left) to go back - FAILED");
//				scsh.makeScreen(
//						"Click on the arrow (bottom-left) to go back - FAILED",
//						screenDir);
//			}
//		} catch (NoSuchElementException e) {
//			System.out
//					.println("Click on the arrow (bottom-left) to go back Exception e - FAILED");
//			file.writeFile("(bottom-left) to go back Exception e - FAILED");
//			scsh.makeScreen("(bottom-left) to go back Exception e - FAILED",
//					screenDir);
//		}
		// read book from "More Like" section
		try {
			hover.moreLike(driver);
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if (hover.doesElementExist(driver, By.xpath("//*[@id=\"reader-wrap\"]")) == true) {
				System.out
						.println("Read book from 'More Like' section - PASSED");
				file.writeFile("Read book from 'More Like' section - PASSED");
				//scsh.makeScreen("Read book from More Like section PASSED",screenDir);
			} else {
				System.out
						.println("Read book from 'More Like' section - FAILED");
				file.writeFile("Read book from 'More Like' section - FAILED");
				scsh.makeScreen("Read book from More Like section FAILED",
						screenDir);
			}
		} catch (NoSuchElementException e) {
			System.out
					.println("Read book from 'More Like' section Exception e - FAILED");
			file.writeFile("Read book from 'More Like' section Exception e - FAILED");
			scsh.makeScreen(
					"Read book from More Like section Exception e - FAILED",
					screenDir);
		}
		driver.navigate().back();
		driver.navigate().back();
//		driver.get("http://staging.membersarea.jomediainc.com/#books");
//		driver.navigate().refresh();
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		// enter to collection
		try {
			driver.findElement(
					By.xpath("//*[@id=\"sidenav-your-stuff\"]/ul/li[1]/a/span[1]"))
					.click();
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if (driver
					.getCurrentUrl()
					.equals("http://staging.membersarea.jomediainc.com/#member/collection")) {
				System.out.println("Open Collection - PASSED");
				file.writeFile("Open Collection - PASSED");
			} else {
				System.out.println("Open Collection - FAILED");
				file.writeFile("Open Collection - FAILED");
				scsh.makeScreen("Open Collection - FAILED", screenDir);
			}
		} catch (NoSuchElementException e) {
			System.out.println("Open Collection Exception e - FAILED");
			file.writeFile("Open Collection Exception e - FAILED");
			scsh.makeScreen("Open Collection Exception e - FAILED", screenDir);
		}

		// read book from collection
		try {
			hover.fromColl(driver);
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if (driver.findElement(By.xpath("//*[@id=\"reader-wrap\"]"))
					.isEnabled()) {
				System.out.println("Read book from collection - PASSED");
				file.writeFile("Read book from collection - PASSED");
				scsh.makeScreen("Read book from collection PASSED", screenDir);
			} else {
				System.out.println("Read book from collection - FAILED");
				file.writeFile("Read book from collection - FAILED");
				scsh.makeScreen("Read book from collection FAILED", screenDir);
			}
		} catch (NoSuchElementException e) {
			System.out
					.println("Read book from collection Exception e - FAILED");
			file.writeFile("Read book from collection Exception e - FAILED");
			scsh.makeScreen("Read book from collection Exception e - FAILED",
					screenDir);
		}
		driver.navigate().back();
		//driver.navigate().back();
//		driver.get("http://staging.membersarea.jomediainc.com/#books");
//		driver.navigate().refresh();
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		driver.findElement(By.id("books_tab")).click();
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		// Click on "read now" button of any book (plus button)
		try {
			hover.addToCollectionMus(driver);
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if (driver.findElement(By.xpath("//*[@id=\"reader-wrap\"]"))
					.isEnabled()) {
				System.out
						.println("Read book by Click on 'read now' button of any book (plus button) - PASSED");
				file.writeFile("Read book by Click on 'read now' button of any book (plus button) - PASSED");
				scsh.makeScreen(
						"Read book by Click on read now button of any book on plus button PASSED",
						screenDir);
			} else {
				System.out
						.println("Read book by Click on 'read now' button of any book (plus button) - FAILED");
				file.writeFile("Read book by Click on 'read now' button of any book (plus button) - FAILED");
				scsh.makeScreen(
						"Read book by Click on read now button of any book on plus button FAILED",
						screenDir);
			}
		} catch (NoSuchElementException e) {
			System.out
					.println("'read now' button of any book (plus button) Exception e - FAILED");
			file.writeFile("'read now' button of any book (plus button) Exception e - FAILED");
			scsh.makeScreen(
					"Read book by Click on read now button of any book on plus button FAILED",
					screenDir);
		}
		driver.navigate().back();
		driver.navigate().back();
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		driver.findElement(By.id("books_tab")).click();
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		// Click on "More info" (plus button)
		try {
			hover.moreInfo(driver);
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if (driver.findElement(
					By.xpath("//*[@id=\"title_download_container\"]/a"))
					.isEnabled()) {
				System.out
						.println("Clicked on 'More info' (plus button) - PASSED");
				file.writeFile("Clicked on 'More info' (plus button) - PASSED");
			}else{
				System.out.println("Clicked on 'More info' (plus button) - FAILED");
				file.writeFile("Clicked on 'More info' (plus button) - FAILED");
				scsh.makeScreen("Clicked on 'More info' (plus button) - FAILED",
						screenDir);
			}
		} catch (NoSuchElementException e) {
			System.out.println("Clicked on 'More info' (plus button) Exception e - FAILED");
			file.writeFile("Clicked on 'More info' (plus button) Exception e - FAILED");
			scsh.makeScreen("Clicked on 'More info' (plus button) Exception e - FAILED",
					screenDir);
		}
//		driver.navigate().back();
//		//driver.navigate().back();
//		try {
//			Thread.sleep(3000);
//		} catch (InterruptedException e) {
//			e.printStackTrace();
//		}
		driver.findElement(By.id("books_tab")).click();
		// Read book from because section
		try {
			hover.becauseSect(driver);
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if (driver.findElement(By.xpath("//*[@id=\"reader-wrap\"]"))
					.isEnabled()) {
				System.out.println("Read book from because section - PASSED");
				file.writeFile("Read book from because section - PASSED");
				scsh.makeScreen("Read book from because section PASSED",
						screenDir);
			}else{
				System.out.println("Read book from because section - FAILED");
				file.writeFile("Read book from because section - FAILED");
				scsh.makeScreen("Read book from because section FAILED", screenDir);
			}
		} catch (NoSuchElementException e) {
			System.out.println("Read book from because section Exception e - FAILED");
			file.writeFile("Read book from because section Exception e - FAILED");
			scsh.makeScreen("Read book from because section Exception e FAILED", screenDir);
		}
		driver.navigate().back();
				
	}
}
